﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.ModuleInitilize;
using Microsoft.Practices.Unity;
using RMS.DataAccess.Common;
using System.Reflection;
using RMS.Utils.Dicom;
using RMS.Utils.EventAggregators;
using RMS.Utils.BackgroundTasks;
using RMS.Utils.RulesManager;
using RMS.Web.Shell.Services;
using RMS.Utils.RoleProvider;
using RMS.Utils.Authentication;
using RMS.Utils.Histogram;

namespace RMS.Web.Shell
{
    public class ShellModuleInitilizer : ModuleInitilizerBase
    {
        public override void OnInitilizeContainer(IUnityContainer container)
        {
            container.RegisterInstance<IEventAggregator>(new EventAggregator());

            Type RepositoryType = typeof(IRepository);
            Type[] allTypes = Assembly.GetAssembly(RepositoryType).GetTypes();
            foreach (Type repository in allTypes.Where(RepositoryType.IsAssignableFrom))
            {
                if (repository.IsAbstract || repository.IsGenericType)
                    continue;

                container.RegisterInstance(repository,
                                           Activator.CreateInstance(repository, Constants.DefaultConnectionString));
            }
            //____________________________________________________________________
           
            container.RegisterType<IBackgroundTask, RulesExecuter>("Rules", new ContainerControlledLifetimeManager());
            container.RegisterType<IBackgroundTask, DicomBootstraperTask>("DicomBootstraperTask", new ContainerControlledLifetimeManager());


            container.RegisterType<IDicomBase,DicomFile>(new ContainerControlledLifetimeManager());
            container.RegisterType<IRulesManager, RulesManager>(new ContainerControlledLifetimeManager());
            container.RegisterType<IAuthentication, AuthenticationService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IRoleProvider, RoleProviderService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IHistogram, Histogram>(new ContainerControlledLifetimeManager());
            container.RegisterType<IRulesManager, RulesManager>(new ContainerControlledLifetimeManager());


            base.OnInitilizeContainer(container);
        }
    }
}
